package com.example.gzysmartcity.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.ThreadUtils;
import com.example.gzysmartcity.repository.model.CarType;
import com.example.gzysmartcity.repository.model.Line;
import com.example.gzysmartcity.repository.model.MyCarBuy;
import com.example.gzysmartcity.repository.remote.State;
import com.example.gzysmartcity.utils.NetworkUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class CarBuyViewModel extends ViewModel {
    MutableLiveData<State> carBuyLiveData=new MutableLiveData<>();
    List<CarType.DataBean> carType;
    List<Line.DataBean> LineList;
    List<MyCarBuy> mCarBuyList;



    public void getCarTypeData(){
        ThreadUtils.executeByCached(new ThreadUtils.Task<CarType>() {
            @Override
            public CarType doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("CarType/getAll?", null, CarType.class, 0);
            }

            @Override
            public void onSuccess(CarType result) {
                carType=result.getData();
                getCarBuyLineData();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {
                carBuyLiveData.postValue(State.FAIL);
            }
        });
    }

    public void getCarBuyLineData(){
        //生产线
        ThreadUtils.executeByCached(new ThreadUtils.Task<Line>() {
            @Override
            public Line doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("Line/getAll?", null, Line.class, 0);
            }

            @Override
            public void onSuccess(Line result) {
                LineList=result.getData();
                getMyCarBuyData();
            }


            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {

            }
        });
    }

    public void getMyCarBuyData() {
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Object>() {
            @Override
            public Object doInBackground() throws Throwable {
                mCarBuyList=new ArrayList<>();
                for(CarType.DataBean carType:carType){
                    String linName=LineList.stream().filter(a->a.getCarTypeId()==carType.getId()).findFirst().get().getLineName();
                    mCarBuyList.add(new MyCarBuy(carType.getCarTypeName(), carType.getPrice()+"",carType.getSize()+"",linName));
                }
                return null;
            }

            @Override
            public void onSuccess(Object result) {
                carBuyLiveData.postValue(State.SUCCESS);
            }
        });
    }

    public List<MyCarBuy> getSingSreach(String name){
        return mCarBuyList.stream().filter(a->a.getCarTypeName().equals(name)).collect(Collectors.toList());
    }

    public List<MyCarBuy> getSingPriceSreach(String name,String p){
        int p1=Integer.valueOf(p)+500;
        int p2=Integer.valueOf(p)-500;
        return mCarBuyList.stream().filter(a->a.getCarTypeName().equals(name)&&
                Integer.valueOf(a.getPrice())>=p2&&Integer.valueOf(a.getPrice())<=p1).collect(Collectors.toList());
    }

    public List<MyCarBuy> getCarBuyList() {
        return mCarBuyList;
    }


    public List<CarType.DataBean> getCarTypeList(){
        return carType;
    }
    public LiveData<State> getcarBuyLiveData(){
        return carBuyLiveData;
    }
}
